{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from src import config, utils\n",
    "import logging\n",
    "from sklearn.model_selection import KFold\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Will drop ['a_feature', 'UserInfo_270'] len=2\n"
     ]
    }
   ],
   "source": [
    "assert config.dataset == 'val'\n",
    "train = pd.read_csv(config.pj_root + 'data/' + config.dataset + '.csv', index_col='no')\n",
    "\n",
    "assert train.columns[0] == 'flag'\n",
    "a_feature = pd.read_csv(config.pj_root + 'data/a_feature.csv', index_col='no')\n",
    "train = train.join(a_feature)\n",
    "\n",
    "if len(config.drop_columns) != 0:\n",
    "    logging.warning('Will drop %s len=%d' % (str(config.drop_columns), len(config.drop_columns)))\n",
    "    train = train.drop(config.drop_columns, axis=1)\n",
    "\n",
    "if len(config.select_columns) != 0:\n",
    "    config.select_columns.insert(0, 'flag')  # use flag in training\n",
    "    logging.warning('Will select %s len=%d' % (str(config.select_columns), len(config.select_columns)))\n",
    "    train = train[train.columns[train.columns.isin(config.select_columns)]]\n",
    "\n",
    "\n",
    "if config.use_basic_process:\n",
    "    train, col_func_map = utils.basic_process(train, has_flag=True)\n",
    "    utils.dump_to_data(col_func_map, 'col_func_map.pkl')\n",
    "\n",
    "X = train.values[:, 1:]\n",
    "Y = train.values[:, 0:1].reshape((-1,))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "kf = KFold(n_splits=config.kfold_k)\n",
    "all_score = 0\n",
    "importance = []\n",
    "for i, (train_index, test_index) in enumerate(kf.split(X)):\n",
    "    X_train, X_test = X[train_index], X[test_index]\n",
    "    y_train, y_test = Y[train_index], Y[test_index]\n",
    "    if i==0: break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\tvalidation_0-auc:0.53795\n",
      "Will train until validation_0-auc hasn't improved in 50 rounds.\n",
      "[1]\tvalidation_0-auc:0.543434\n",
      "[2]\tvalidation_0-auc:0.520606\n",
      "[3]\tvalidation_0-auc:0.522101\n",
      "[4]\tvalidation_0-auc:0.527786\n",
      "[5]\tvalidation_0-auc:0.535201\n",
      "[6]\tvalidation_0-auc:0.549991\n",
      "[7]\tvalidation_0-auc:0.554826\n",
      "[8]\tvalidation_0-auc:0.573066\n",
      "[9]\tvalidation_0-auc:0.576484\n",
      "[10]\tvalidation_0-auc:0.581455\n",
      "[11]\tvalidation_0-auc:0.590514\n",
      "[12]\tvalidation_0-auc:0.583002\n",
      "[13]\tvalidation_0-auc:0.58386\n",
      "[14]\tvalidation_0-auc:0.58629\n",
      "[15]\tvalidation_0-auc:0.591989\n",
      "[16]\tvalidation_0-auc:0.592775\n",
      "[17]\tvalidation_0-auc:0.595433\n",
      "[18]\tvalidation_0-auc:0.598494\n",
      "[19]\tvalidation_0-auc:0.606668\n",
      "[20]\tvalidation_0-auc:0.602198\n",
      "[21]\tvalidation_0-auc:0.599878\n",
      "[22]\tvalidation_0-auc:0.593204\n",
      "[23]\tvalidation_0-auc:0.594029\n",
      "[24]\tvalidation_0-auc:0.596629\n",
      "[25]\tvalidation_0-auc:0.602172\n",
      "[26]\tvalidation_0-auc:0.604238\n",
      "[27]\tvalidation_0-auc:0.603985\n",
      "[28]\tvalidation_0-auc:0.602646\n",
      "[29]\tvalidation_0-auc:0.604739\n",
      "[30]\tvalidation_0-auc:0.59967\n",
      "[31]\tvalidation_0-auc:0.597305\n",
      "[32]\tvalidation_0-auc:0.603244\n",
      "[33]\tvalidation_0-auc:0.60132\n",
      "[34]\tvalidation_0-auc:0.605427\n",
      "[35]\tvalidation_0-auc:0.604414\n",
      "[36]\tvalidation_0-auc:0.605011\n",
      "[37]\tvalidation_0-auc:0.607702\n",
      "[38]\tvalidation_0-auc:0.607117\n",
      "[39]\tvalidation_0-auc:0.605869\n",
      "[40]\tvalidation_0-auc:0.608365\n",
      "[41]\tvalidation_0-auc:0.611978\n",
      "[42]\tvalidation_0-auc:0.612458\n",
      "[43]\tvalidation_0-auc:0.6093\n",
      "[44]\tvalidation_0-auc:0.611757\n",
      "[45]\tvalidation_0-auc:0.61138\n",
      "[46]\tvalidation_0-auc:0.610899\n",
      "[47]\tvalidation_0-auc:0.612341\n",
      "[48]\tvalidation_0-auc:0.612237\n",
      "[49]\tvalidation_0-auc:0.609937\n",
      "[50]\tvalidation_0-auc:0.61008\n",
      "[51]\tvalidation_0-auc:0.611965\n",
      "[52]\tvalidation_0-auc:0.609755\n",
      "[53]\tvalidation_0-auc:0.609937\n",
      "[54]\tvalidation_0-auc:0.611289\n",
      "[55]\tvalidation_0-auc:0.613446\n",
      "[56]\tvalidation_0-auc:0.612393\n",
      "[57]\tvalidation_0-auc:0.61277\n",
      "[58]\tvalidation_0-auc:0.610899\n",
      "[59]\tvalidation_0-auc:0.611562\n",
      "[60]\tvalidation_0-auc:0.611874\n",
      "[61]\tvalidation_0-auc:0.613433\n",
      "[62]\tvalidation_0-auc:0.612523\n",
      "[63]\tvalidation_0-auc:0.611445\n",
      "[64]\tvalidation_0-auc:0.611692\n",
      "[65]\tvalidation_0-auc:0.610548\n",
      "[66]\tvalidation_0-auc:0.60891\n",
      "[67]\tvalidation_0-auc:0.609443\n",
      "[68]\tvalidation_0-auc:0.609755\n",
      "[69]\tvalidation_0-auc:0.610431\n",
      "[70]\tvalidation_0-auc:0.607663\n",
      "[71]\tvalidation_0-auc:0.607429\n",
      "[72]\tvalidation_0-auc:0.606604\n",
      "[73]\tvalidation_0-auc:0.608332\n",
      "[74]\tvalidation_0-auc:0.606565\n",
      "[75]\tvalidation_0-auc:0.607734\n",
      "[76]\tvalidation_0-auc:0.606318\n",
      "[77]\tvalidation_0-auc:0.605577\n",
      "[78]\tvalidation_0-auc:0.604056\n",
      "[79]\tvalidation_0-auc:0.602354\n",
      "[80]\tvalidation_0-auc:0.602653\n",
      "[81]\tvalidation_0-auc:0.602549\n",
      "[82]\tvalidation_0-auc:0.601327\n",
      "[83]\tvalidation_0-auc:0.600495\n",
      "[84]\tvalidation_0-auc:0.601392\n",
      "[85]\tvalidation_0-auc:0.60238\n",
      "[86]\tvalidation_0-auc:0.602042\n",
      "[87]\tvalidation_0-auc:0.602406\n",
      "[88]\tvalidation_0-auc:0.601028\n",
      "[89]\tvalidation_0-auc:0.602458\n",
      "[90]\tvalidation_0-auc:0.600482\n",
      "[91]\tvalidation_0-auc:0.600248\n",
      "[92]\tvalidation_0-auc:0.600755\n",
      "[93]\tvalidation_0-auc:0.600313\n",
      "[94]\tvalidation_0-auc:0.600625\n",
      "[95]\tvalidation_0-auc:0.599261\n",
      "[96]\tvalidation_0-auc:0.598793\n",
      "[97]\tvalidation_0-auc:0.599222\n",
      "[98]\tvalidation_0-auc:0.598403\n",
      "[99]\tvalidation_0-auc:0.597909\n",
      "[100]\tvalidation_0-auc:0.598052\n",
      "[101]\tvalidation_0-auc:0.59839\n",
      "[102]\tvalidation_0-auc:0.59878\n",
      "[103]\tvalidation_0-auc:0.598403\n",
      "[104]\tvalidation_0-auc:0.599092\n",
      "[105]\tvalidation_0-auc:0.599637\n",
      "Stopping. Best iteration:\n",
      "[55]\tvalidation_0-auc:0.613446\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=1,\n",
       "       gamma=0, learning_rate=0.01, max_delta_step=0, max_depth=5,\n",
       "       min_child_weight=100, missing=None, n_estimators=200, nthread=-1,\n",
       "       objective='rank:pairwise', reg_alpha=0, reg_lambda=1,\n",
       "       scale_pos_weight=1, seed=0, silent=True, subsample=0.75)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config.model_para = dict(\n",
    "    max_depth=5,\n",
    "    n_estimators=200,\n",
    "    base_score=0.5,\n",
    "    learning_rate=0.01,\n",
    "    objective='rank:pairwise',\n",
    "    min_child_weight=100,\n",
    "    subsample=0.75,\n",
    "    # silent=False\n",
    ")\n",
    "\n",
    "model = config.model(**config.model_para)\n",
    "model.fit(X_train, y_train, eval_metric='auc', eval_set=[(X_test, y_test)], early_stopping_rounds=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14.228426395939087"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(y_train[y_train==0]) / len(y_train[y_train==1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
